package com.carrental.amap.service;

import cn.hutool.core.net.Ipv4Util;
import cn.hutool.core.net.NetUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.carrental.amap.domain.IpAddressInfo;
import com.carrental.amap.enums.AmapApiEnum;
import com.carrental.amap.utils.ApiUtils;
import com.carrental.common.utils.ip.AddressUtils;
import com.carrental.framework.config.AmapConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author admin
 * @since 2022/3/25 11:29
 */
@Service
@Slf4j
public class AmapService {
    @Autowired
    private AmapConfig amapConfig;
    @Autowired
    private ApiUtils apiUtils;
    @Autowired
    private HttpServletRequest httpServletRequest;

    /**
     * 天河区ip
     */
    public Map<String,Object> weather() {
        Map<String, Object> param = new HashMap<>(16);
        log.info("枚举" + AmapApiEnum.WEATHER_API.getAction() + ":" + AmapApiEnum.WEATHER_API.getUrl());
        param.put("key", amapConfig.getWebApiKey());
        param.put("city", getIpInfo().getAdcode());
        param.put("extensions", "all");
        System.out.println("请求参数"+param);
        //高德地图天气WEATHER_API("GET","https://restapi.amap.com/v3/weather/weatherInfo?parameters");
        return JSONObject.parseObject(HttpUtil.get(AmapApiEnum.WEATHER_API.getUrl(), param));
    }

    public IpAddressInfo getIpInfo() {
        Map<String, Object> param = new HashMap<>(16);
        param.put("key", amapConfig.getWebApiKey());
        //高德ip查询IP_API("GET", "https://restapi.amap.com/v3/ip?parameters"),
        return JSONObject.parseObject(HttpUtil.get(AmapApiEnum.IP_API.getUrl(), param),IpAddressInfo.class);
    }
}
